package simu.nodes;

import java.io.File;
import java.util.ArrayList;

/**
 * Singleton class, create nodes using a factory methods. 
 * @author Edwin Flórez G.
 *
 */
public class MasterNode {
	
	private static final String SYSTEM_FOLDER_NAME = "systemFiles"; 	//Default system folder, contains sub-folder for each node.
	private static MasterNode myInstance; 								//Contains the only object of this class
	protected static ArrayList<Node> myNodes = new ArrayList<Node>(); 	//List of nodes
	protected int nodePositionID;										//Keep track nodes
	
    // Constants to define the node's type.
	// Processing Node type, 
	private static final int PROC_NODE = 1;
    // The simples one node, just a ALIX with one WIFI interface  
    private static final int SIMPLE_NODE = 2;
    // ALIX with two WIFI interface, one in Ad-Hoc mode and other in AP mode.
    private static final int AP_NODE = 3;

	/**
	 * Default constructor, is private to prevent be instantiated for external class.
	 */
	private MasterNode() {
		this.nodePositionID = 0;
		File f = new File(SYSTEM_FOLDER_NAME);
		//Check if folder exist, if not create.
		if (!f.exists()){
			f.mkdir();
		}
	}
	
	/**
	 * To instanced the only object in this class
	 * @return myInstance the only object of this class
	 */
	public static MasterNode getInstance(){
		if (myInstance == null)
			myInstance = new MasterNode();
		return myInstance;
	}
	
	/**
	 * Factory method, creates nodes of type typeNode
	 * @param typeNode (ProcNode, SimpleNode, APNode)
	 */
	public void addNode(int typeNode){
		switch (typeNode) {
		case PROC_NODE: 	
			myNodes.add(new ProcNode(nodePositionID++));
			break;
		case SIMPLE_NODE:	
			myNodes.add(new SimpleNode(nodePositionID++));
			break;
		case AP_NODE: 		
			myNodes.add(new APNode(nodePositionID++));
			break;
		}
	}
	
	public Node getNode(int index){
		if (index < 0 || index > myNodes.size())
			throw new IllegalArgumentException(index+" is not a valid node.");
		return myNodes.get(index);
	}
	
	public ArrayList<Node> getNodes(){
		return myNodes;
	}
	
	public String[] getFiles(int index){
		return myNodes.get(index).getData();
	}

	public ArrayList<String[]> getAllFiles(){
		ArrayList<String[]> result = new ArrayList<String[]>();
		for (int i = 0; i < myNodes.size(); i++){
			result.add(getFiles(i));
		}
		return result;
	}
	
	
	public int size(){
		return this.nodePositionID;
	}
	
}